#include "PlayLayer.h"
#include "CardInfo.h"
#include "BellSprite.h"
#include "CardSprite.h"
#include "ShelfSprite.h"
#include "ComboProgress.h"
#include "PauseLayer.h"
#include "OverLayer.h"
#include "AudioManage.h"

static const char* FONT_BMP = "fonts/helvetica-32.fnt";
static const float MOVE_TIME = 0.5f;
static const float DELAY_TIME = 0.5f;
static const char* CONST_PNT_COMBO_1 = "Progress/combo1.png";
static const char* CONST_PNT_COMBO_2 = "Progress/combo2.png";
static const char* CONST_PNT_COMBO_3 = "Progress/combo3.png";
static const char* CONST_PNT_COMBO_4 = "Progress/combo4.png";
static const char* CONST_PNT_COMBO_5 = "Progress/combo5.png";
static const char* CONST_PNG_COMBO_MAX = "Progress/comboMax.png";
static const char* CONST_PNT_COMBOS[5] = { CONST_PNT_COMBO_1, CONST_PNT_COMBO_2, CONST_PNT_COMBO_3, CONST_PNT_COMBO_4, CONST_PNT_COMBO_5 };
static const char* CONST_PNG_PAUSE_N = "Pause/pause_N.png";
static const char* CONST_PNG_PAUSE_S = "Pause/pause_S.png";
static const int INITIAL_SCORE = 0;
static const int INITIAL_TIME = 30;

static int myKeys[5];
static Label* timeLabel;
static Label* scoreLabel;
static Label* addLabel;
static int _time = INITIAL_TIME;
static int _score = INITIAL_SCORE;
static Sprite* comboSprite;
static MenuItemImage* pauseItem;

PlayLayer::PlayLayer()
{
}


PlayLayer::~PlayLayer()
{
    __NotificationCenter::getInstance()->removeAllObservers(this);
    unscheduleAllCallbacks();
}

PlayLayer* PlayLayer::thisLayer;

Scene* PlayLayer::createScene()
{
	Scene* playScene = Scene::create();
	Layer* playLayer = PlayLayer::create();

	playScene->addChild(playLayer);
	thisLayer = (PlayLayer*) playLayer;

	return playScene;
}

PlayLayer* PlayLayer::getInstance()
{
	return thisLayer;
}

bool PlayLayer::init()
{
	if (!Layer::init())
	{
		return false;
	} 

	Size visibleSize = Director::getInstance()->getVisibleSize();

	Sprite* back = Sprite::create("backGround.jpg");
	back->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
	this->addChild(back);

    CardSprite* cardSprite = CardSprite::create();
    cardSprite->setScale(0.5f);
    cardSprite->setPosition(Vec2(visibleSize.width / 2, visibleSize.height * 0.75f));
    this->addChild(cardSprite);
    
    CardSprite::setPublicCard(cardSprite);

    ShelfSprite* shelf = ShelfSprite::getInstance();
    shelf->setScale(0.4f);
    shelf->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 12));
    this->addChild(shelf);

    int* kp = shelf->keys;

    BellSprite* bell = new BellSprite(kp);
    bell->setScale(0.3f);
    bell->setPosition(Vec2(visibleSize.width / 2, visibleSize.height * 0.23));
    this->addChild(bell, 10);

    ComboProgress::getInstance()->setPosition(Vec2(visibleSize.width / 2, visibleSize.height * 0.93f));
    this->addChild(ComboProgress::getInstance());

    scoreLabel = Label::createWithBMFont(FONT_BMP, "Score:0");
    scoreLabel->setPosition(Vec2(visibleSize.width * 0.18f , visibleSize.height * 0.88f));
    scoreLabel->setScale(0.6f);
    this->addChild(scoreLabel);

    char t[20] = {""};
    sprintf(t, "Time:%d", _time);

    timeLabel = Label::createWithBMFont(FONT_BMP, t);
    timeLabel->setPosition(Vec2(visibleSize.width * 0.82f, visibleSize.height * 0.88f));
    timeLabel->setScale(0.6f);
    this->addChild(timeLabel);

    addLabel = Label::createWithBMFont(FONT_BMP, "");
    addLabel->setScale(0.58f);
    addLabel->setVisible(false);
    this->addChild(addLabel);

    comboSprite = Sprite::create();
    comboSprite->setPosition(Vec2(visibleSize.width / 2, visibleSize.height * 0.7f));
    comboSprite->setVisible(false);
    this->addChild(comboSprite, 10);

    pauseItem = MenuItemImage::create(CONST_PNG_PAUSE_N,CONST_PNG_PAUSE_S,
        CC_CALLBACK_0(PlayLayer::pauseCallBack, this));
    pauseItem->setPosition(Vec2(visibleSize.width * 0.9f, visibleSize.height * 0.8f));
    pauseItem->setScale(0.5f);

    Menu* playMenu = Menu::create(pauseItem, NULL);
    playMenu->setPosition(Vec2::ZERO);
    this->addChild(playMenu, 10);

    schedule(schedule_selector(PlayLayer::ComboTimer), 0.01f, kRepeatForever, 0);
    schedule(schedule_selector(PlayLayer::Timer), 1.0f, kRepeatForever, 0);

    __NotificationCenter::getInstance()->addObserver(this, callfuncO_selector(PlayLayer::onHomeButtonPressed), EVENT_HOME_BUTTON_PRESSED, nullptr);
    
	AudioManage::playPlayMusic();

	return true;
}

void PlayLayer::ComboTimer(float dt)
{
	if (!ComboProgress::subProgress())
	{
		ComboProgress::newProgress();
	}
}

void PlayLayer::Timer(float dt)
{
	if (_time > 0)
	{
		--_time;

		char t[20] = { "" };
		sprintf(t, "Time:%d", _time);
		timeLabel->setString(t);
	}
	else
	{
		//unschedule(schedule_selector(PlayLayer::Timer));

		AudioManage::stopPlayMusic();
		AudioManage::playEndMusic();

		OverLayer::showLayer(_score);
	}
}

void PlayLayer::addScore()
{
	_score += 1;

	char s[20] = {""};
	sprintf(s, "Score:%d", _score);

	scoreLabel->setString(s);
}

void PlayLayer::subScore()
{
	if (_score > 0)
	{
		--_score;

		char s[20] = { "" };
		sprintf(s, "Score:%d", _score);

		scoreLabel->setString(s);
	}
}

void PlayLayer::addTime(int value)
{
	char addt[5];
	sprintf(addt, "+ %d", value);
	addLabel->setString(addt);
	addLabel->setPosition(Vec2( timeLabel->getPosition().x * 1.05f,timeLabel->getPosition().y * 0.95f));
	addLabel->runAction(Sequence::create(
		Show::create(),
		Spawn::create(
		MoveTo::create(MOVE_TIME, timeLabel->getPosition()),
		FadeOut::create(MOVE_TIME),
		CallFunc::create([value]{
		_time += value;
		
		char t[20] = { "" };
		sprintf(t, "Time:%d", _time);
		timeLabel->setString(t);
		}),

		NULL),
		Hide::create(),
		FadeIn::create(0.0f),
		NULL
		));
		
		if (value > 5)
		{
			comboSprite->setTexture(CONST_PNG_COMBO_MAX);
			value = 5;
		}
		else
		{
			comboSprite->setTexture(CONST_PNT_COMBOS[value - 1]);
		}

		comboSprite->runAction(Sequence::create(
			FadeOut::create(0.0f),
			Show::create(),
			FadeIn::create(MOVE_TIME / 2),
			DelayTime::create(DELAY_TIME),
			FadeOut::create(MOVE_TIME / 2),
			Hide::create(),
			NULL
			));
}

void PlayLayer::pauseCallBack()
{
	PauseLayer::showLayer();
}

void PlayLayer::stopGame()
{
	AudioManage::pausePlayMusic();

	unschedule(schedule_selector(PlayLayer::Timer));
	unschedule(schedule_selector(PlayLayer::ComboTimer));
}

void PlayLayer::continueGame()
{
	AudioManage::continuePlayMusic();

	schedule(schedule_selector(PlayLayer::ComboTimer), 0.01f, kRepeatForever, 0);
	schedule(schedule_selector(PlayLayer::Timer), 1.0f, kRepeatForever, 0);
}

void PlayLayer::disposeLayer()
{
	_score = INITIAL_SCORE;
	_time = INITIAL_TIME;

	ShelfSprite::disposeSprite();
	CardSprite::disposeSprite();
	ComboProgress::disposeSprite();
	PauseLayer::disposeLayer();
	OverLayer::disposeLayer();
}

void PlayLayer::onHomeButtonPressed(Ref* )
{
    pauseCallBack();
}
